/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DataViewer.java
 *
 * Created on Oct 24, 2009, 2:38:19 PM
 */
package wekimini.dtw.gui;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import wekimini.learning.dtw.DtwData;
import wekimini.util.Util;

/**
 *
 * @author rebecca
 */
public class DtwDataViewer extends javax.swing.JFrame {

    private final DtwData dtwData;
    private static final Logger logger = Logger.getLogger(DtwDataViewer.class.getName());

    public DtwDataViewer(DtwData data) {
        initComponents();
        this.dtwData = data;
        setComboBoxOptions();
        showAllOutputs();
    }

    private void setComboBoxOptions() {        
        String[] options = new String[dtwData.getNumGestures()+ 1];
        options[0] = "All outputs";
        for (int i = 0; i < dtwData.getNumGestures(); i++) {
            options[i+1] = "Show only examples for " + dtwData.getGestureName(i);
        }
        
        DefaultComboBoxModel m = new DefaultComboBoxModel(options);
        comboWhichOutputs.setModel(m);
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        scrollTable = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        buttonDone = new javax.swing.JButton();
        buttonSaveArff = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        comboWhichOutputs = new javax.swing.JComboBox();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        textArea.setColumns(20);
        textArea.setRows(5);
        scrollTable.setViewportView(textArea);

        buttonDone.setText("Done");
        buttonDone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDoneActionPerformed(evt);
            }
        });

        buttonSaveArff.setText("Save to CSV...");
        buttonSaveArff.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSaveArffActionPerformed(evt);
            }
        });

        jLabel2.setText("Show:");

        comboWhichOutputs.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All gesture types", "Only Output_1" }));
        comboWhichOutputs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboWhichOutputsActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(scrollTable, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 675, Short.MAX_VALUE)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(0, 0, Short.MAX_VALUE)
                        .add(buttonSaveArff)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(buttonDone))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(comboWhichOutputs, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(scrollTable, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(buttonDone)
                    .add(buttonSaveArff))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(comboWhichOutputs, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonDoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDoneActionPerformed
        this.dispose();
    }//GEN-LAST:event_buttonDoneActionPerformed

    private void buttonSaveArffActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveArffActionPerformed
            File file = Util.findSaveFile("csv",
                    "data",
                    "csv file",
                    this);
            if (file != null) {
                try {
                    dtwData.writeInstancesToCSV(file);
                } catch (Exception ex) {
                    Logger.getLogger(DtwDataViewer.class.getName()).log(Level.WARNING, null, ex);
                    Util.showPrettyErrorPane(this, "Could not save to file: " + ex.getMessage());
                }
            }
    }//GEN-LAST:event_buttonSaveArffActionPerformed

    private void comboWhichOutputsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboWhichOutputsActionPerformed
        int selected = comboWhichOutputs.getSelectedIndex();
        if (selected == 0) {
           showAllOutputs();
        } else {
            showOnlyGesture(selected-1);
        }
        //TODO: May need to repaint

    }//GEN-LAST:event_comboWhichOutputsActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    }//GEN-LAST:event_formWindowClosing

    public void showOnlyGesture(int which) {
        String s = dtwData.toDataString(which);
        textArea.setText(s);
    }
    
    /*private File findArffFileToSave() throws IOException {
     return null;
     //TODOTODOTODO: handle this after get fileext support in.
     } */
    /**
     * @param args the command line arguments
     */
   /* public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                boolean isDiscrete[] = {true, false};
                int numVals[] = {3, 3};
                //String featureNames[] = {"F1", "f2", "F3", "f4", "f5"};
                String featureNames[] = new String[100];
                for (int i = 0; i < featureNames.length; i++) {
                    featureNames[i] = "F" + i;
                }
                String paramNames[] = {"P1", "p2"};
                SimpleDataset s = new SimpleDataset(featureNames.length, 2, isDiscrete, numVals, featureNames, paramNames);

                new DatasetViewer(s).setVisible(true);
            }
        });
    } */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonDone;
    private javax.swing.JButton buttonSaveArff;
    private javax.swing.JComboBox comboWhichOutputs;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane scrollTable;
    private javax.swing.JTextArea textArea;
    // End of variables declaration//GEN-END:variables


    private void showAllOutputs() {
        String s = dtwData.toDataString();
        textArea.setText(s);
    }
}
